import { nextTick } from "vue"

export const useForm = () => {

    //清空内容
    function handleClear(formItem, jsondata, tableType = 'head') {
        console.log(1123)
        formItem.value = "";
        if (formItem.valuehidden) {
            formItem.valuehidden = "";
        }
        // 映射清空
        if (formItem.fieldmap !== '' && jsondata) {
            const mapFieldArr = formItem.fieldmap.split(";").map(item => {
                return item.split(":")[1]
            }).filter(item => item)


            if (tableType === 'head') {
                for (let i = 0; i < jsondata.head.length; i++) {
                    for (let j = 0; j < jsondata.head[i].groupvalue.length; j++) {
                        const fitem = jsondata.head[i].groupvalue[j]
                        if (mapFieldArr.indexOf(fitem.sField) !== -1) {
                            nextTick(() => {
                                fitem.value = ""
                                if (fitem.valuehidden) {
                                    fitem.valuehidden = ""
                                }
                            })
                        }
                    }
                }
            }

            if (tableType === 'body') {
                                                                console.log(666663)

                for (let bodydatakey in jsondata.bodydata) {
                    let body = jsondata.body[bodydatakey]
                    let bodydata = jsondata.bodydata[bodydatakey]
                    for (let i = 0; i < bodydata.length; i++) {
                        for (let j = 0; j < bodydata[i].length; j++) {
                            const fitem = bodydata[i][j]
                            if (mapFieldArr.indexOf(fitem.sField) !== -1) {
                                nextTick(() => {
                                    fitem.value = ""
                                    if (fitem.valuehidden) {
                                        fitem.valuehidden = ""
                                    }
                                })
                            }

                        }
                    }
                }
            }

        }

    }


    // 字段值发生变化
    function handleChange(item, value, valuehidden = "", fieldmap = {}, mapobject = "", tableType = "head", bodyDataName = "", bodyDataIndex = "") {
        item.value = value;
        if (valuehidden) {
            item.valuehidden = valuehidden;
        }

        //字段联动
        handleFiledLink(item, jsondata.value, orginData.value);

        //字段映射(和插入点)
        if (fieldmap && mapobject) {
            handleFiledMap(jsondata, fieldmap, mapobject, tableType, bodyDataName, bodyDataIndex);
        }
    }
    return {
        handleClear
    }


}